package com.cloudfun.campusshare.repo;

import com.cloudfun.campusshare.entity.UserRoleEntity;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.Collection;
import java.util.List;

/**
 * Created by Huqin on 2019/12/23
 */
public interface UserRoleEntityRepo extends
        PagingAndSortingRepository<UserRoleEntity, String>,
        JpaSpecificationExecutor<UserRoleEntity> {

    List<UserRoleEntity> findAllByUserId(String userIds);

    List<UserRoleEntity> findAllByRoleIdIn(Collection<String> roleIds);

    List<UserRoleEntity> findAllByRoleId(String roleId);

    void deleteByUserId(String userId);

    /**
     * 查找对应角色的用户id
     * @param roleName
     * @return
     */
    @Query(value = "select ur.user_id from user_role ur left join role r on ur.role_id=r.id  where r.name like ?1",nativeQuery = true)
    List<String> findAllUserIdsByRoleName(String roleName);
}
